
$(function() {

	$("#mold").autocomplete(Test.mold);
	
	$('#date1').datepicker(_date1_option);
	$('#date1').change(function() {
		$('#date2').val($(this).val());
	});

	$('#date2').datepicker(_date1_option);
	
	Tip.supplier();

	$('#supplier').blur(function() {
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#tmp_company").val(json.fullname);
			$("#tmp_address").val(json.address);
			
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {			
			abc.linkman(json);
		});
	});

	$('textarea').css("width", 400);
	
	$("input[id='emp']:visible").autocomplete(Test.emp);
	
	_ck_default("input[id='emp']:visible", "input[id^='emp_t']:visible");
});

var abc = {	
	
	linkman: function (a) {		
		$select = $("#abc_linkman");		
		$select.empty();
		count = 0;
		$.each(a, function(k, v) {
	    	var item = new Option(v.name, v.id);
	    	$select[0].options.add(item);
	    	
	    	if (count == 1) {
	    		abc._link_man(v.id, false);
	    	}
	    	
	    	count++;
	    });
		
		$select.change(function() {
			var id = $(this).val();
			abc._link_man(id, true);
		}); 
	},
	
	_link_man: function (id, select) {
		var url = 'info.php?do=Contact.ajax_id&q=' + id;
		$.getJSON(url, function(json) {
			if (select) {
				mobile = '';
				if (!is_null(json.mobile)) {
					mobile = json.mobile;
					
					$("#mobile").val(mobile);
				} else {
					tel = '';
					if (!is_null(json.tel)) {
						tel = json.tel;
						$("#mobile").val(tel);
					}
				}
				
				var company = $("#tmp_company").val();
				var address = $("#tmp_address").val();
				if (!is_null(json.address)) {
					address = json.address;
				}
				
				fmt = "{0}\n{1}";
				str = fmt.format(address, company);
				$("#address").val(str);

				$("#recipient").val(json.name);
			}
		});
	}
}

